import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:signature/signature.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/res/w_dimens.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/views/home/view/drive/signature_A_page.dart';
import 'package:yuan_hang_app/widget/status_btn.dart';
import 'package:yuan_hang_app/widget/w_text.dart';
import '../../../../base/base_state.dart';
import '../../../../http/api.dart';
import '../../../../http/http_utils.dart';
import '../../../../utils/router_helper.dart';
import '../../../../widget/common_dialog.dart';
import '../../../../widget/w_toast.dart';

class SignaturePage extends StatefulWidget {
  Map driveInfo;
   SignaturePage({super.key,  required this.driveInfo});

  @override
  BaseState<SignaturePage> createState() => _SignaturePageState();
}

class _SignaturePageState extends BaseState<SignaturePage> {
  Uint8List? imageBytes;
  final SignatureController _signatureController = SignatureController(
    penStrokeWidth: 3.0.px,
    penColor: Colors.black,
  );

  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
      title: "乙方电子签名",
      leftCallBack: () {
        Navigator.pop(context, imageBytes);
      },
      child: Padding(
          padding: EdgeInsets.only(left: 10.px, right: 10.px, top: 10.px),
          child: Column(children: [
            Container(
              height: 250,
              margin: EdgeInsets.all(10.px),
              padding: EdgeInsets.all(10.px),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10.px)),
              child: Signature(
                controller: _signatureController,
                backgroundColor: WColors.CD5D5D5,
              ),
            ),
            WText(
              "您正在签署试驾协议签名，确认授权电子签名吗？",
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: WColors.hintTextColor, fontSize: WDimens.tsMedium),
            ),
            SizedBox(
              height: 10.px,
            ),
            StatusBtn(
              text: "授权签名",
              isBg: true,
              height: 48.px,
              fontSize: WDimens.tsMedium,
              margin: EdgeInsets.symmetric(horizontal: 20.px),
              onPressed: _saveSignature,
            ),
            SizedBox(
              height: 20.px,
            ),
            StatusBtn(
              text: "重置",
              fontSize: WDimens.tsMedium,
              height: 48.px,
              margin: EdgeInsets.symmetric(horizontal: 20.px),
              onPressed: () {
                _signatureController.clear();
                imageBytes = null;
                setState(() {});
              },
            ),
            SizedBox(
              height: 10.px,
            ),
            // imageBytes == null
            //     ? const SizedBox()
            //     : Image.memory(
            //         imageBytes!,
            //         height: 100,
            //       ),
          ])),
    );
  }

  _saveSignature() async {
    imageBytes = await _signatureController.toPngBytes(); // 获取签名图片
    print("=============长度=${imageBytes?.length}");
    if (imageBytes?.length!=null) {
     //调用方法进行生成签名
      var driveInfo = Map.from(widget.driveInfo);
      driveInfo["description"]=imageBytes.toString();
          CommonDialog.showConformDialog(
            context: context,
            title: "提示",
            content: "即将进行甲方签名，是否继续",
            onConfirm: (text) {
              RouteHelper.pushWidget(context,
                      SignatureAPage(driveInfo: driveInfo))
                  .then((value) {
                if (value != null) {
                  RouteHelper.maybePop(context, value);
                }
                setState(() {});
              });
            },
          );


    }else{
      Toast.toast("请先签名");
    }

    setState(() {});
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _signatureController.dispose();
  }
}
